/* Copyright Andy Mitchell 2007. No warranties */

function ConversationUI(gtdinboxPage){
	this._gtdinboxPage = gtdinboxPage;
	this._doc = gtdinboxPage.doc;
	this._labels = gtdinboxPage._labels;
	this._multiToOne = new MultiToOne(300);
	
	this._eventObservers = new EventObservers(this);
	
	this._labels.addEventHandler( this, "actionlist-manual-click", this.manualThreadLabelChange );
	//this._labels.addEventHandler( this, "refresh-labels", this.refreshLabels );
	
}

ConversationUI.prototype.viewChange = function(gmail, callback){
try{
	// Make sure we're still embedded
	
	var doc = this._gtdinboxPage.gmailDOM.element("document");
	
	var match = this._doc.location.href.match(/\/#inbox\/(.+)$/);
	if( !match ) match = this._doc.location.href.match(/\/#search\/.+\/(.+)$/);
	this.thread = match? this._gtdinboxPage._threadList.getThreadById(match[1]) : "";
	if( !this.thread ){
		if( !callback ) setTimeout( GIPROTOTYPE.bind(this, function(){
				this.viewChange(gmail, true);	
			}), 1000);
		return;
	}
	
	this.thread.initEventHandler();
	this.thread.eventHandlers.reset();	// get it exclusively for us
	/*
	this.thread.addEventHandler(this, "addLabel", this.threadUpdate);
	this.thread.addEventHandler(this, "removeLabel", this.threadUpdate);
	this.thread.addEventHandler(this, "update-body", this.threadUpdate);
	*/
	
	// Label Selection Header
	if( this.elH1 ){
		this._eventObservers.stopObserving( this.elH1, "click", this._clickHeader, false );
		this._eventObservers.stopObserving( this.elH1, "DOMNodeInserted", this._modifyHeader, false );
	}
	var result = this._doc.evaluate("//div[@gtdviewid='" + this._gtdinboxPage.view("gtdviewid") + "']//td[@class='eWTfhb']//h1[@class='YfMhcb']", doc.body, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
	this.elH1 = result.singleNodeValue;
	if( !this.elH1 ) return;
	this._eventObservers.observe( this.elH1, "click", this._clickHeader, false );
	this._eventObservers.observe( this.elH1, "DOMNodeInserted", this._modifyHeader, false );
	
	var elHeader = this.elH1.parentNode;
	if( elHeader.getElementsByTagName("DIV")[0] && elHeader.getElementsByTagName("DIV")[0].className.indexOf("conversation-header")>-1 ){	// already modified
		return;
	}
	
	var elConversationHeader = this._doc.createElement("DIV");
		elConversationHeader.className = "conversation-header";
	elHeader.appendChild( elConversationHeader );
	
	if( !this._labelSelect ){
		this._labelSelect = new LabelSelectUI( this._gtdinboxPage );
		this._labelSelect.addEventHandler(this, "click-label", this.toggleLabel);
		
		this._showHiderLabels = new Showhider({doc: this._doc, title:'Labels', elContent: this._labelSelect.el});	// TODO language
		this._showHiderLabels.addEventHandler(this, "show", this.refreshLabels);
	}
	elConversationHeader.appendChild( this._showHiderLabels.el );
	//this.refreshLabels();
	
}catch(e){
	if( GTDDEBUGMODE ) alert("conversationui viewChange\n" + e.toString());
}
};

ConversationUI.prototype._modifyHeader = function(event){
	this.multiToOne.process("modifyHeader", this.refreshLabels);
};
ConversationUI.prototype._clickHeader = function(event){
	var el = GIPROTOTYPE.element(event);
	
	if( this._gtdinboxPage.gmailDOM.test("conversation-x",el) ){	// It's a remove
		this.refreshLabels();
	}
	
};

ConversationUI.prototype.refreshLabels = function(){
try{
	this._gtdinboxPage._threadListUI.uncheckOutOfView();
	if( this.thread ) this.thread.verifyLabels( this._labels.getActiveLabels() );
	this._labelSelect.refresh(this.thread);
}catch(e){
	if( GTDDEBUGMODE ) alert("conversation ui refreshLabels\n" + e.toString());
}
};
/*
ConversationUI.prototype.threadUpdate = function(){	
try{
	this.thread.verifyLabels( this._labels.getActiveLabels() );
	if( this._gtdinboxPage.view("view")!="cv" ) return;
	this._labelSelect.refresh(this.thread);
	this._labelSelect.newLabels( this.thread.getUnknownBodyLabels() );
}catch(e){
	if( GTDDEBUGMODE ) alert("conversation ui threadUpdate\n" + e.toString());
}
};
*/
ConversationUI.prototype.manualThreadLabelChange = function(event, label){
	// Called when user manually selects label on drop down
	// Kept seperate from Labels because ThreadList/Conversation would handle it differently

try{
	if( this._gtdinboxPage.view("view")!="cv" ) return;	// i should be active view

	if( label.act=="12" ){
		this.thread.addLabel(label.label);
	}else if(label.act=="13"){
		this.thread.removeLabel(label.label);
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("ConversationUI manualThreadLabelChange\n" + e.toString());
}
};

ConversationUI.prototype.toggleLabel = function(e, label){
try{
	var labelname = label.labelname;
	var fCallback = GIPROTOTYPE.bind(this, function(act){
			if( act > 0 ){
				this._labelSelect.selected( this.thread.labels );
			}
		});
	this._gtdinboxPage._labels.labelThread({'labelname':labelname, 'on':undefined, 'threads':this.thread, 'fCallback':fCallback});
}catch(e){
	if( GTDDEBUGMODE ) alert("conversationui toggleLabel\n" + e.toString());
}
};

